package com.xinqi.modules.user.users.third.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.modules.user.third.dto.rsp.UserThirdCheckBackResultDTO;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.user.users.third.domain.UserThirdBindEntity;
import com.xinqi.modules.user.users.third.manager.UserThirdBindManager;
import com.xinqi.modules.user.users.third.mapper.UserThirdBindMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 第三方用户绑定表管理层实现
 *
 * @author: yyzhen
 * @date: 2023/07/17
 */
@Service
@RequiredArgsConstructor
public class UserThirdBindManagerImpl extends BaseServiceImpl<UserThirdBindMapper, UserThirdBindEntity> implements UserThirdBindManager {

    @Override
    public UserThirdCheckBackResultDTO thirdLoginCheck(String type, String identification) {
        return this.baseMapper.thirdLoginCheck(type, identification);
    }

    /**
     * 根据用户ID获取第三方绑定信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserThirdBindEntity> findByUserId(Long userId) {
        LambdaQueryWrapper<UserThirdBindEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserThirdBindEntity::getUserId, userId);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * 根据type获取用户的第三方绑定信息
     *
     * @param type
     * @param userId
     * @return
     */
    @Override
    public UserThirdBindEntity findByTypeAndUserId(String type, Long userId) {
        LambdaQueryWrapper<UserThirdBindEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserThirdBindEntity::getType, type)
            .eq(UserThirdBindEntity::getUserId, userId)
            .last("limit 1");

        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 通过用户ID删除
     *
     * @param userId
     */
    @Override
    public void deleteByUserId(Long userId) {
        this.baseMapper.delete(new LambdaQueryWrapper<UserThirdBindEntity>().eq(UserThirdBindEntity::getUserId, userId));
    }

}
